﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;

namespace BookM
{
    public partial class BorrowReturnM : Form
    {
        public BorrowReturnM()
        {
            InitializeComponent();
        }

        #region 对象
        BorrowReturnMBLL borrowReturnMBLL = new BorrowReturnMBLL();
        #endregion

        /// <summary>
        /// 根据读者id搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_search_Click(object sender, EventArgs e)
        {
            //根据读者编号搜索
            Search();
        }

        /// <summary>
        /// 搜索实现方法
        /// </summary>
        private void Search()
        {
            Dictionary<string, string> Reader = borrowReturnMBLL.getReturn(txt_Bookid.Text);
            if (txt_Bookid.Text != "")
            {
                if (Reader.Count > 0)
                {
                    //赋值
                    lb_ReaderID.Text = Reader[EnumTools.GetDescription(ManageEnums.READER.READER_ID)];
                    lb_ReaderName.Text = Reader[EnumTools.GetDescription(ManageEnums.READER.READER_NAME)];
                    //获取读者借书列表
                    Dgv_Return.DataSource = borrowReturnMBLL.getBorrowReturnList(txt_Bookid.Text);
                }
            }
            else
            {
                lb_ReaderID.Text = "";
                lb_ReaderName.Text = "";
                Dgv_Return.DataSource = borrowReturnMBLL.getBorrowReturnList(null);
            }
        }

        /// <summary>
        /// 根据图书id查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_confirm_Click(object sender, EventArgs e)
        {
             Dgv_booklist.DataSource = borrowReturnMBLL.getBookList(txb_BookID.Text);
        }

        /// <summary>
        /// 窗体加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BorrowReturnM_Load(object sender, EventArgs e)
        {
            //图书列表
            Dgv_booklist.DataSource = borrowReturnMBLL.getBookList(null);

            //获取读者借书列表
            Dgv_Return.DataSource = borrowReturnMBLL.getBorrowReturnList(null);
            NewMethod();
        }

        /// <summary>
        /// 禁止自动生成行列
        /// </summary>
        private void NewMethod()
        {
            Dgv_booklist.AutoGenerateColumns = false;//不让datagridview自动生成列，可在属性中修改
            Dgv_booklist.AllowUserToAddRows = false;//禁止自动生成行可在属性中修改
            Dgv_Return.AutoGenerateColumns = false;//不让datagridview自动生成列，可在属性中修改
            Dgv_Return.AllowUserToAddRows = false;//禁止自动生成行可在属性中修改
        }

        /// <summary>
        /// 借书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_borrowbooks_Click(object sender, EventArgs e)
        {
            borrowbooks();
        }
    
        /// <summary>
        /// 还书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            borrowReturn();
        }

        /// <summary>
        /// 还书方法
        /// </summary>
        private void borrowReturn()
        {
            int index = Dgv_Return.SelectedCells[0].RowIndex;
            var BookID = Dgv_Return.Rows[index].Cells["编号"].Value;
            var BorrowReturnID = Dgv_Return.Rows[index].Cells["ID"].Value;
            if (BookID!= null && BorrowReturnID!= null)
            {
                if (MessageBox.Show(" 确认要还编号：‘" + BookID + "’ 这本书？", " 还书 ", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (borrowReturnMBLL.ReturnBook(BookID.ToString(), BorrowReturnID.ToString()))
                    {
                        MessageBox.Show(EnumTools.GetDescription(ManageEnums.BORROW.RETURN_SUCCEED));
                        Refresh();
                        if (lb_ReaderID.Text!="")
                        {
                            Dgv_Return.DataSource = borrowReturnMBLL.getBorrowReturnList(lb_ReaderID.Text);
                        }
                        else
                        {
                            Dgv_Return.DataSource = borrowReturnMBLL.getBorrowReturnList(null);
                        }
                        Dgv_booklist.DataSource = borrowReturnMBLL.getBookList(null);
                    }
                    else
                    {
                        MessageBox.Show(EnumTools.GetDescription(ManageEnums.BORROW.RETURN_DEFEATED));
                    }
                }
            }
               
        }

        /// <summary>
        /// 借书方法
        /// </summary>
        private void borrowbooks()
        {
            int index = Dgv_booklist.SelectedCells[0].RowIndex;
            var BookID = Dgv_booklist.Rows[index].Cells["编号"].Value;
            var BookName = Dgv_booklist.Rows[index].Cells["图书名称"].Value;
            if (BookName!= null && BookID != null)
            {
                if (MessageBox.Show(" 确认借：‘" + BookName + "’ 这本书？", " 借书 ", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string succeed = EnumTools.GetDescription(ManageEnums.BORROW.SUCCEED);
                    string re = borrowReturnMBLL.AddBorrow(BookID.ToString());
                    if (re.Equals(succeed))
                    {
                        MessageBox.Show(re);
                        Refresh();
                        Dgv_booklist.DataSource = borrowReturnMBLL.getBookList(null);
                        Dgv_Return.DataSource = borrowReturnMBLL.getBorrowReturnList(lb_ReaderID.Text);
                    }
                    else
                    {
                        MessageBox.Show(re);
                    }
                }
            }
        }

    }
}
